#!/usr/bin/env python
def mail_setup(cmdstr, cmdargs):
    '''Sets up mail for the user.'''
    
    mail_providers = {"Gmail / Google Mail":"http://www.gmail.com",
                      "Yahoo":"http://mail.yahoo.com",
                      "Hotmail":"http://www.hotmail.com",
                      "Hushmail":"http://www.hushmail.com/",
                      }
    
    print("Mail Setup")
    print("Select the type of mail client you have:")
    
    i = 0
    for k in mail_providers.keys():
        print ("    %s) %s" % (i,k))
        i+=1
    
    print('''    a) System
    b) Other online
''')
    j = raw_input("? ")
    
    if j == 'a':
        print("Enter the command to start your mail client:")
        k = raw_input("? ")
        registry.store("mail.isaprogram", True)
        registry.store("mail.client", k)
    else:
        if j == 'b':
            print("Enter the URL of your mail client:")
            k = raw_input("? ")
            
            if "//" not in k:   #If not http[s] or other protocol, gopher anyone?
                k = "http://%s" % (k)
        else:
            try:
                j = int(j)
                k = mail_providers.values()[j]
            except:
                print("Error reading your input!")
                k = ""
    
        registry.store("mail.isaprogram", False)
        registry.store("mail.client", k)
    
def mail(cmdstr, cmdargs):
    '''Opens the mail client for the user.'''
    #Check if mail is setup or not.
    if not registry.fetch("mail.client", True):  #Mail is set up
        mail_setup(cmdstr, cmdargs)
    
    #Fetch the mail.
    if not registry.fetch("mail.isaprogram", False):
        open_url(registry.fetch("mail.client", ""))
    else:
        client = registry.fetch("mail.client", "")
        print("Opening:%s" % (client))
        call(client)

TRIGGERS = {
            "email":mail,
            "mail":mail,
            "mail setup":mail_setup,
            "email setup":mail_setup,
            }